package com.dingding.common.core.util.paravalidate;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;

import com.dingding.common.core.base.code.BaseCodeMessage;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.util.ToolUtil;

/**
 * @Description:验证一个bean里是否有为空字段
 * @author fengshuonan
 * @date 2016年7月27日 上午10:21:16
 * @version 叮叮搭车
 */
public class ValidateUtil {

	private static Logger log = Logger.getLogger(ValidateUtil.class);

	/**
	 * @Description: 验证请求bean是否可用
	 * @param obj
	 *            被验证的bean
	 * @param clazz
	 *            被验证bean的类类型
	 * @author fengshuonan
	 * @return true 可用 false 不可用
	 */
	public static ValiResult validateBeanIsAvaliable(Object obj, Class<?> clazz) {
		ValiResult valiResult = new ValiResult();
		if (obj == null) {
			valiResult.setValue(false, "allField");
			return valiResult;
		}
		Method[] methods = clazz.getDeclaredMethods();
		try {
			for (Method method : methods) {
				String methodName = method.getName();
				if (!methodName.startsWith("get")) {
					continue;
				}
				Field field = clazz.getDeclaredField(ToolUtil.firstLetterToLowwer(methodName.substring(3)));
				String fieldName = field.getName();
				CanNull anno = field.getAnnotation(CanNull.class);
				if (anno != null) {
					continue;
				}
				Object returnObj = method.invoke(obj);
				if (returnObj == null) {
					valiResult.setValue(false, fieldName);
					return valiResult;
				}
				if (returnObj instanceof String) {
					if (ToolUtil.isBlank(returnObj.toString())) {
						valiResult.setValue(false, fieldName);
						valiResult.getNullFieldList().add(fieldName);
						continue;
					}
				}
			}
		} catch (Exception e) {
			log.error("反射验证bean的参数是否为空时出错！ValidateUtil ：validateBeanIsAvaliable()", e);
		}
		
		if(valiResult.getNullFieldList() != null && valiResult.getNullFieldList().size() > 0){
			valiResult.setAvailable(false);
		}else{
			valiResult.setAvailable(true);
		}
		
		return valiResult;
	}

	/**
	 * @Description：验证请求bean是否为空
	 * @param requestBean 请求bean的封装
	 * @author fengshuonan
	 * @param <T>
	 * @return DubboInvokResponseDTO<?> 返回服务调用的封装bean
	 */
	public static ResponseEntity<?> validateReqPara(Object requestBean) {

		@SuppressWarnings("rawtypes")
		ResponseEntity response = new ResponseEntity();

		if (requestBean == null) {
			response.setCode(HttpStatus.SC_FORBIDDEN);
			return response;
		}

		ValiResult result = ValidateUtil.validateBeanIsAvaliable(requestBean, requestBean.getClass());

		if (!result.isAvailable()) {
			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(BaseCodeMessage.DATA_NULL_PARAM_MESSAGE);
		}

		return response;
	}

	/**
	 * @Description:验证一个bean是否有为空字段
	 * @author fengshuonan
	 * @return true 没有空字段，可以使用    false 有空字段，不能使用
	 * @since 2.3.2
	 */
	public static boolean validateBean(Object obj, Class<?> clazz) {

		ValiResult result = ValidateUtil.validateBeanIsAvaliable(obj, clazz);

		if (result.isAvailable() == false) {
			log.info("为空的字段是：" + result.getNullFieldName());
		}

		return result.isAvailable();
	}

}
